package com.huluobo.a2203parkdemo.visitor

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.app.TimePickerDialog
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.DatePicker
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.huluobo.a2203parkdemo.R
import com.huluobo.a2203parkdemo.adapter.VisitorAdapter
import com.huluobo.a2203parkdemo.databinding.ActivityVisitorBinding
import com.huluobo.base.bean.VisitorBean
import com.huluobo.base.bean.VisitorBeanItem
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class VisitorActivity : BaseActivity<ActivityVisitorBinding, VisitorViewModel>() {
    private lateinit var visitorAdapter: VisitorAdapter
    private var visitorList = mutableListOf<VisitorBeanItem>()
    private lateinit var visitorBeanItem: VisitorBeanItem
    override fun initData() {
        val map = mutableMapOf<String, String>()
        map[Const.PARAM_CAR_NUM] = ""
        val json = Gson().toJson(map)
        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
        viewModel.getAllVisitor(body)

        viewModel.getAllVisitorSuccess.observe(this) {
            visitorList = Gson().fromJson(it, VisitorBean::class.java)
            visitorAdapter.data.addAll(visitorList)
            visitorAdapter.notifyDataSetChanged()
        }

        viewModel.deleteVisitorSuccess.observe(this) {
            visitorList.remove(visitorBeanItem)
            visitorAdapter.remove(visitorBeanItem)
        }
    }

    override fun initView() {
        binding.visitorHeader.headerBack.visibility = View.VISIBLE
        binding.visitorHeader.headerBack.setOnClickListener { finish() }
        binding.visitorHeader.headerAdd.visibility = View.VISIBLE
        binding.visitorHeader.headerAdd.setOnClickListener {
            //新增来访信息
            val intent = Intent(this, AddVisitorActivity::class.java)
            startActivity(intent)
        }
        binding.visitorHeader.headerTitle.text = "全部来访"

        binding.visitorRv.layoutManager = LinearLayoutManager(this)
        visitorAdapter = VisitorAdapter()
        binding.visitorRv.adapter = visitorAdapter

        visitorAdapter.addChildClickViewIds(R.id.item_visitor_delete)
        visitorAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.item_visitor_delete -> {
                    val map = mutableMapOf<String, Int>()
                    map[Const.PARAM_VISITOR_ID] = visitorList[position].visitorId
                    val json = Gson().toJson(map)
                    val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                    viewModel.deleteVisitor(body)
                    visitorBeanItem = visitorList[position]
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        val map = mutableMapOf<String, String>()
        map[Const.PARAM_CAR_NUM] = ""
        val json = Gson().toJson(map)
        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
        viewModel.getAllVisitor(body)
    }
}